package com.duoec.dw.lineage.service.analyser.statement;

import com.duoec.dw.lineage.dto.SqoopCommendArgument;
import com.duoec.dw.lineage.dto.statement.ExportSqoopStatement;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuwenzhen
 */
@Service
public class ExportSqoopStatementAnalyser extends BaseSqoopStatementAnalyser<ExportSqoopStatement> {
    private static final String PREFIX = "export";

    /**
     * 检查当前行是否需要当前分析器处理
     *
     * @param line 当前行
     * @return true，则会使用analyse方法解析
     */
    @Override
    public boolean check(String line) {
        String trimLine = line.trim();
        if (trimLine.startsWith(STR_SQOOP)) {
            line = trimLine.substring(STR_SQOOP.length()).trim();
        }
        return line.startsWith(PREFIX);
    }

    /**
     * 解析脚本，并返回当前的语句
     *
     * @param lines 所有行
     * @param index 开始行，0开始
     * @return 当前语句
     */
    @Override
    public ExportSqoopStatement analyse(List<String> lines, int index) {
        //export --connect jdbc:vertica://10.50.23.208:5433/duoec_esf_dw --driver com.vertica.jdbc.Driver --username dbadmin --password-file /user/oozie/share/lib/lib_20151229093730/password_vertica_pro.file --table duoec_trade_center_edw_source.s_du_orders --hcatalog-database duoec_trade_center_edw_source --hcatalog-table s_du_orders --hcatalog-partition-keys dt --hcatalog-partition-values ${dt} --batch --skip-dist-cache
        ExportSqoopStatement statement = new ExportSqoopStatement();
        String line = lines.get(index);
        SqoopCommendArgument args = getArgument(line);
        statement.setCommend(line);
        statement.setArgs(args);
        statement.setStart(index);
        statement.setEnd(index);
        return statement;
    }
}
